#include "Stdafx.h"
#include "PduData.h"

using namespace SNMP;

PduData::PduData(void){}

PduData::PduData(HSNMP_SESSION hSession, HSNMP_PDU hPdu)
{
	this->m_hPdu = hPdu;
	this->m_hSession = hSession;
}

PduData::~PduData(void)
{
	SnmpFreePdu(this->m_hPdu);
}

void PduData::Clear(void)
{
	SnmpFreePdu(this->m_hPdu);
	//SNMPAPI_STATUS status = SnmpFreePdu(this->m_hPdu);
	/*
	if(status == SNMPAPI_FAILURE)
	{
		status = SnmpGetLastError(NULL);
		SNMPManager::ErrorManage(status);
	}
	*/
}

void PduData::CreatePduHandle(HSNMP_SESSION hSession, smiINT pduType, smiINT32 requestID, smiINT errorStatus, smiINT errorIndex, SNMP::VariableBindingsList^ varbindlist)
{
	this->m_vblVbl = varbindlist;
	this->CreatePduHandle(hSession, pduType, requestID, errorStatus, errorIndex, varbindlist->m_hVbl);
}

void PduData::CreatePduHandle(HSNMP_SESSION hSession, smiINT pduType, smiINT32 requestID, smiINT errorStatus, smiINT errorIndex, HSNMP_VBL varbindlist)
{
	this->m_hSession = hSession;
	this->m_intRequestId = requestID;
	this->m_intErrorStatus = errorStatus;
	this->m_intErrorIndex = errorIndex;
	this->m_hPdu = SnmpCreatePdu(hSession, pduType, requestID, errorStatus, errorIndex, varbindlist);
	this->m_pduType = PduData::GetPduType(pduType);
	this->m_vblVbl->m_hVbl = varbindlist;
	if(this->m_hPdu == SNMPAPI_FAILURE)
	{
		SNMPAPI_STATUS status = SnmpGetLastError(hSession);
		SNMPManager::ErrorManage(status);
	}
}

void PduData::GetPduData(void)
{
	long PDU_type;
	long request_id;
	long error_status;
	long error_index;
	HSNMP_VBL varbindlist;
	SNMPAPI_STATUS status = SnmpGetPduData(this->m_hPdu, &PDU_type, &request_id, &error_status, &error_index, &varbindlist);

	if(status == SNMPAPI_FAILURE)
	{
		status = SnmpGetLastError(this->m_hSession);
		SNMPManager::ErrorManage(status);
	}

	this->m_intRequestId = request_id;
	this->m_intErrorStatus = error_status;
	this->m_intErrorIndex = error_index;
	this->m_pduType = PduData::GetPduType(PDU_type);
	this->m_vblVbl = gcnew SNMP::VariableBindingsList(this->m_hSession, varbindlist);
}

PduData::PduType PduData::GetPduType(int value)
{
	switch (value)
	{
	case SNMP_PDU_GET:
		return PduData::PduType::Get;
		break;
	case SNMP_PDU_GETBULK:
		return PduData::PduType::GetBulk;
		break;
	case SNMP_PDU_GETNEXT:
		return PduData::PduType::GetNext;
		break;
	case SNMP_PDU_RESPONSE:
		return PduData::PduType::Response;
		break;
	case SNMP_PDU_SET:
		return PduData::PduType::Set;
		break;
	case SNMP_PDU_TRAP:
		return PduData::PduType::Trap;
		break;
	default:
		return PduData::PduType::Get;
		break;
	}
}

signed long PduData::GetPduType(PduData::PduType value)
{
	switch(value)
	{
	case PduData::PduType::Get:
		return SNMP_PDU_GET;
		break;
	case PduData::PduType::GetBulk:
		return SNMP_PDU_GETBULK;
		break;
	case PduData::PduType::GetNext:
		return SNMP_PDU_GETNEXT;
		break;
	case PduData::PduType::Response:
		return SNMP_PDU_RESPONSE;
		break;
	case PduData::PduType::Set:
		return SNMP_PDU_SET;
		break;
	case PduData::PduType::Trap:
		return SNMP_PDU_TRAP;
		break;
	default:
		return SNMP_PDU_GET;
		break;
	}
}